package com.ggjj.morethreadtransactioninsert.service.impl;

import com.ggjj.morethreadtransactioninsert.entities.TestPo;
import com.ggjj.morethreadtransactioninsert.mapper.TestMapper;
import com.ggjj.morethreadtransactioninsert.service.ITestService;
import com.ggjj.morethreadtransactioninsert.utils.MoreThreadTransactionUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestServiceImpl implements ITestService {
    @Resource
    private TestMapper testMapper;
    //常量一万
    public static final int _1W = 10000;
    //静态变量数据
    public static List<TestPo> testPoList = getData();
    //获取一万条静态变量数据方法
    private static List<TestPo> getData(){
        int userSize = _1W * 10;
        List<TestPo> list = new ArrayList<>(userSize);
        for (long i = 1; i <= userSize; i++) {
            list.add(TestPo.builder().id(i).name("用户-"+i).build());
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void singleThreadInsert() {
        //调用mapper层向五张表分别插入10w数据
        testMapper.batchInsert("t_test_1",testPoList);
        testMapper.batchInsert("t_test_2",testPoList);
        testMapper.batchInsert("t_test_3",testPoList);
        testMapper.batchInsert("t_test_4",testPoList);
        testMapper.batchInsert("t_test_5",testPoList);
    }
    @Resource
    private PlatformTransactionManager platformTransactionManager;
    @Override
    public void moreThreadInsert() {
        // 事务多线程事务，向 5 张表插入 20 万条数据
        boolean result = MoreThreadTransactionUtils.execute(platformTransactionManager,
                () -> testMapper.batchInsert("t_test_1", testPoList),
                () -> testMapper.batchInsert("t_test_2", testPoList),
                () -> testMapper.batchInsert("t_test_3", testPoList),
                () -> testMapper.batchInsert("t_test_4", testPoList),
                () -> testMapper.batchInsert("t_test_5", testPoList));
        //所有的插入是否都成功了
        if (result){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
    }

    @Override
    public void moreThreadInsertFail() {
// 事务多线程事务，向 5 张表插入 20 万条数据
        boolean success = MoreThreadTransactionUtils.execute(this.platformTransactionManager,
                () -> this.testMapper.batchInsert("t_test_1", testPoList),
                () -> this.testMapper.batchInsert("t_test_2", testPoList),
                () -> this.testMapper.batchInsert("t_test_3", testPoList),
                () -> this.testMapper.batchInsert("t_test_4", testPoList),

                //插入第5表的时候，故意使坏手动丢个异常，让多线程事务都回滚
                () -> {
                    this.testMapper.batchInsert("t_test_5", testPoList);
                    throw new RuntimeException("手动抛出异常，模拟失败场景，最后一个表的插入会报异常，导致 5 张表数据都会被回滚");
                });

        // 所有的插入是否都成功了？
        if (success) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }
    }

    @Override
    public void delete() {
        for (int i = 1; i <= 5; i++) {
            String tableName = "t_test_" + i;
            testMapper.delete(tableName);
        }
    }
}
